package Year22_Month06.day17;

import java.util.LinkedList;
import java.util.Queue;

public class 左叶子之和_迭代写法 {
    public int sumOfLeftLeaves(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) return 0;
        queue.offer(root);
        int val = 0;//用来统计所有的左叶子之和
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size-- > 0){
                TreeNode node = queue.poll();
                //如果当前节点的左子树为叶子结点，那么计入它的值
                if(node.left != null && node.left.left == null && node.left.right == null) val += node.left.val;
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
        }
        return val;
    }
}
